package com.ym.jiujiangeyou.bitmap;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.annotation.TargetApi;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
import android.support.v4.util.LruCache;

/** 
* @author 叶铭
* @Description: 图片工具类 
*/
public class BitmapUtil {

	private static BitmapUtil bitmapUtil;
	private LruCache<String, Bitmap> lruCache;
	private  static int requestSize = 120;

	public static BitmapUtil getInstance() {
		if (bitmapUtil == null) {
			bitmapUtil = new BitmapUtil();
		}
		return bitmapUtil;
	}

	
	
	/** 
	* @param     
	* @throws 
	* @Description: 实例化缓存对象 
	*/
	public BitmapUtil() {
		int maxMemory = (int) Runtime.getRuntime().maxMemory();
		int maxSize = maxMemory / 8;
		lruCache = new LruCache<String, Bitmap>(maxSize) {
			@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
			@Override
			protected int sizeOf(String key, Bitmap value) {
				// TODO Auto-generated method stub
			//	return value.getByteCount();
				return value.getRowBytes() * value.getHeight() ;
			}
		};
	}

	/** 
	* @param key
	* @param bitmap  
	* @retur  void 
	* @Description: 放入缓存 
	*/
	public void addBitmapToLrucache(String key, Bitmap bitmap) {
		if (key == null || bitmap == null) {
			return;
		}
		if (getBitmapFromLrucache(key) == null) {
			lruCache.put(key, bitmap);
		}
	}

	/** 
	* @param key
	* @return  
	* @retur  Bitmap 
	* @Description: 取出缓存对象 
	*/
	public Bitmap getBitmapFromLrucache(String key) {
		if (key == null) {
			return null;
		}
		return lruCache.get(key);
	}
	
	
	/***
	 * 图片的缩放方法
	 * 
	 * @param bgimage
	 *            ：源图片资源
	 * @param newWidth
	 *            ：缩放后宽度
	 * @param newHeight
	 *            ：缩放后高度
	 * @return
	 */
	public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
			double newHeight) {
		// 获取这个图片的宽和高
		float width = bgimage.getWidth();
		float height = bgimage.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		// 计算宽高缩放率
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
				(int) height, matrix, true);
		return bitmap; 
	}
	
	/** 
	* @param options
	* @param reqWidth
	* @return  
	* @retur  int 
	* @Description: 计算比例
	*/
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth) {
		// 源图片的宽度
		final int width = options.outWidth;
		int inSampleSize = 1;
		if (width > reqWidth) {
			// 计算出实际宽度和目标宽度的比率
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = widthRatio;
		}
		return inSampleSize;
	}

	/** 
	* @param pathName
	* @param reqWidth
	* @return  
	* @retur  Bitmap 
	* @Description: 压缩图片
	*/
	public static Bitmap decodeSampledBitmapFromResource(String pathName,
			int reqWidth) {
		// 第一次解析将inJustDecodeBounds设置为true，来获取图片大小
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(pathName, options);
		// 调用上面定义的方法计算inSampleSize值
		options.inSampleSize = calculateInSampleSize(options, reqWidth);
		// 使用获取到的inSampleSize值再次解析图片
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(pathName, options);
	}

	
	
	/** 
	* @param f
	* @return  
	* @retur  Bitmap 
	* @Description: 压缩图片 
	*/
	public static Bitmap decodeFile(File f) {
		try {
			// decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(new FileInputStream(f), null, o);
			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			int scale = 1;
			while (true) {
				if (width_tmp / 2 < requestSize
						|| height_tmp / 2 < requestSize)
					break;
				width_tmp /= 2;
				height_tmp /= 2;
				scale *= 2;
			}	
			// decode with inSampleSize
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
		} catch (FileNotFoundException e) {
		}
		return null;
	}
	

}
